$(function () {
    getOutBoundsList()
})

/**
 * 获得所出库信息
 */
function getOutBoundsList() {
    $.ajax({
        type:"post",
        url:"/OutBoundManage/getOutBoundListAfterPage",
        data:{},
        dataType:"json",
        success:function(r){
            if (r.data != null){
                var t="";
                for(var i=0;i<r.data.length;i++){
                    t+="<tr outboundid='"+r.data[i].outboundid+"'>\n" +
                        "          <td>"+(i+1)+"</td>\n" +
                        "          <td class='orderid'>"+r.data[i].orderid+"</td>\n" +
                        "          <td>￥"+r.data[i].totalprice+"</td>\n" +
                        "          <td>"+r.data[i].storagename+"</td>\n" +
                        "          <td>"+r.data[i].outboundtime+"</td>\n" +
                        "          <td>"+r.data[i].outboundman+"</td>\n" +
                        "          <td stateid='"+r.data[i].stateid+"'>"+r.data[i].state+"</td>\n" +
                        "          <td>\n";
                    if(r.data[i].state==="未发货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定此订单发货吗？')\">发货</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已发货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单回款吗？')\">确认回款</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已回款"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="取消订单"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单的退货吗？')\">确认已退货</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已退货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "          </td>\n";
                    }
                         t+= "        </tr>\n";
                }
                $(".tablelist tbody").html(t);
                var pageNum = document.getElementById("pageNum");
                pageNum.innerHTML=r.pageNum
                var totalcomment = document.getElementById("totalcomment");
                totalcomment.innerHTML=r.totalRows
                totalPage=r.totalPage;
                getpagemodel(totalPage);
            }
        },
        error:function (r) {
            alert("!!!")
        }

    })
}

var totalPage;
//上一页
function shangyiye() {
    var pagenum = document.getElementById("pageNum");
    var n=pagenum.innerHTML
    if (n>1){
        n--;
        $.ajax({
            type:"post",
            url:"/OutBoundManage/getOutBoundListAfterPage",
            data:{pagenum:n},
            dataType:"json",
            success:function(r){
                if (r.data != null){
                    var t="";
                    for(var i=0;i<r.data.length;i++){
                        t+="<tr outboundid='"+r.data[i].outboundid+"'>\n" +
                            "          <td>"+(i+1)+"</td>\n" +
                            "          <td class='orderid'>"+r.data[i].orderid+"</td>\n" +
                            "          <td>￥"+r.data[i].totalprice+"</td>\n" +
                            "          <td>"+r.data[i].storagename+"</td>\n" +
                            "          <td>"+r.data[i].outboundtime+"</td>\n" +
                            "          <td>"+r.data[i].outboundman+"</td>\n" +
                            "          <td stateid='"+r.data[i].stateid+"'>"+r.data[i].state+"</td>\n" +
                            "          <td>\n";
                        if(r.data[i].state==="未发货"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定此订单发货吗？')\">发货</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="已发货"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单回款吗？')\">确认回款</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="已回款"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="取消订单"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单的退货吗？')\">确认已退货</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="已退货"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "          </td>\n";
                        }
                        t+= "        </tr>\n";
                    }
                    $(".tablelist tbody").html(t);
                    var pageNum = document.getElementById("pageNum");
                    pageNum.innerHTML=r.pageNum;
                    var totalcomment = document.getElementById("totalcomment");
                    totalcomment.innerHTML=r.totalRows;
                    totalPage=r.totalPage;
                    getpagemodel(totalPage);
                }
            },
            error:function (r) {
                alert("!!!")
            }

        })
    }else{
        alert("已经是第一页了")
    }
}
//下一页
function xiayiye() {
    var pagenum = document.getElementById("pageNum");
    var n=pagenum.innerHTML
    if (n<totalPage){
        n++;
        $.ajax({
            type:"post",
            url:"/OutBoundManage/getOutBoundListAfterPage",
            data:{pagenum:n},
            dataType:"json",
            success:function(r){
                if (r.data != null){
                    var t="";
                    for(var i=0;i<r.data.length;i++){
                        t+="<tr outboundid='"+r.data[i].outboundid+"'>\n" +
                            "          <td>"+(i+1)+"</td>\n" +
                            "          <td class='orderid'>"+r.data[i].orderid+"</td>\n" +
                            "          <td>￥"+r.data[i].totalprice+"</td>\n" +
                            "          <td>"+r.data[i].storagename+"</td>\n" +
                            "          <td>"+r.data[i].outboundtime+"</td>\n" +
                            "          <td>"+r.data[i].outboundman+"</td>\n" +
                            "          <td stateid='"+r.data[i].stateid+"'>"+r.data[i].state+"</td>\n" +
                            "          <td>\n";
                        if(r.data[i].state==="未发货"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定此订单发货吗？')\">发货</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="已发货"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单回款吗？')\">确认回款</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="已回款"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="取消订单"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单的退货吗？')\">确认已退货</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="已退货"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                                "          </td>\n";
                        }
                        t+= "        </tr>\n";
                    }
                    $(".tablelist tbody").html(t);
                    var pageNum = document.getElementById("pageNum");
                    pageNum.innerHTML=r.pageNum;
                    var totalcomment = document.getElementById("totalcomment");
                    totalcomment.innerHTML=r.totalRows;
                    totalPage=r.totalPage;
                    getpagemodel(totalPage);
                }
            },
            error:function (r) {
                alert("!!!")
            }

        })
    }else{
        alert("已经是最后一页了")
    }
}
//渲染页数
function getpagemodel(n) {
    var t="<li class=\"paginItem\"><a href=\"javascript:;\" onclick=\"shangyiye()\"><span class=\"pagepre\"></span></a></li>\n";
    for(var i=1;i<=n;i++){
        t+="<li class=\"paginItem\"><a href=\"javascript:;\" onclick='choiseModel(this)'>"+i+"</a></li>\n";
    }
    t+="<li class=\"paginItem\"><a href=\"javascript:;\" onclick=\"xiayiye()\"><span class=\"pagenxt\"></span></a></li>";
    $(".paginList").html(t);
}
//选择页码去到分页
function choiseModel(n) {
    var pagenum = $(n).html();
    $.ajax({
        type:"post",
        url:"/OutBoundManage/getOutBoundListAfterPage",
        data:{pagenum:pagenum},
        dataType:"json",
        success:function(r){
            if (r.data != null){
                var t="";
                for(var i=0;i<r.data.length;i++){
                    t+="<tr outboundid='"+r.data[i].outboundid+"'>\n" +
                        "          <td>"+(i+1)+"</td>\n" +
                        "          <td class='orderid'>"+r.data[i].orderid+"</td>\n" +
                        "          <td>￥"+r.data[i].totalprice+"</td>\n" +
                        "          <td>"+r.data[i].storagename+"</td>\n" +
                        "          <td>"+r.data[i].outboundtime+"</td>\n" +
                        "          <td>"+r.data[i].outboundman+"</td>\n" +
                        "          <td stateid='"+r.data[i].stateid+"'>"+r.data[i].state+"</td>\n" +
                        "          <td>\n";
                    if(r.data[i].state==="未发货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定此订单发货吗？')\">发货</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已发货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单回款吗？')\">确认回款</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已回款"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="取消订单"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单的退货吗？')\">确认已退货</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已退货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "          </td>\n";
                    }
                    t+= "        </tr>\n";
                }
                $(".tablelist tbody").html(t);
                var pageNum = document.getElementById("pageNum");
                pageNum.innerHTML=r.pageNum;
                var totalcomment = document.getElementById("totalcomment");
                totalcomment.innerHTML=r.totalRows;
                totalPage=r.totalPage;
                getpagemodel(totalPage);
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

//去到订单详情界面
function toOrderDetailView(n){
    var orderid = $(n).parent().parent().children("td[class='orderid']").html();
    $.ajax({
        type:"post",
        url:"/OutBoundManage/toCommentDeliveryView",
        data:{orderid:orderid},
        dataType:"json",
        success:function(r){
            location.href="/storage/delivery/deliveryView2.html";
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

/**
 * 打开提示框并且传入库存的id
 * @param n
 * @param content
 */
function tipOpen1(n,content) {
    var outboundid = $(n).parent().parent().attr("outboundid");
    var stateid = $(n).parent().parent().children("td[stateid]").attr("stateid");
    $(".tipright p").text(content);
    $("#tip").fadeIn(200);
    $.ajax({
        type:"post",
        url:"/OutBoundManage/tochangeOutBoundState",
        data:{outboundid:outboundid,action:"tipOpen1",stateid:stateid},
        dataType:"json",
        success:function(r){
            if(r=="false"){
                alert("错!!!");
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

function tipOpen2(n,content) {
    var outboundid = $(n).parent().parent().attr("outboundid");
    var stateid = $(n).parent().parent().children("td[stateid]").attr("stateid");
    $(".tipright p").text(content);
    $("#tip").fadeIn(200);
    $.ajax({
        type:"post",
        url:"/OutBoundManage/tochangeOutBoundState",
        data:{outboundid:outboundid,action:"tipOpen2",stateid:stateid},
        dataType:"json",
        success:function(r){
            if(r=="false"){
                alert("错!!!");
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })

}

/**
 * 关闭提示框的同时改变仓库状态
 */
function tipClose1() {
    $("#tip").fadeOut(200);
    $.ajax({
        type:"post",
        url:"/OutBoundManage/changeOutBoundState",
        dataType:"json",
        success:function(r){
            if(r=="false"){
                alert("错!!!");
            }
            location.href="deliveryList.html";
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

function tipClose2() {
    $("#tip").fadeOut(200);
}

//查询功能实现
function check() {
    var checkhouse =$(".tools>li:nth-child(1)>input:first-child").val();
    var ordernumber=$(".tools>li:nth-child(2)>input:first-child").val();
    var state=$(".tools>li:nth-child(3)>select:nth-child(1)").val();
    if(state=="请选择"){
        state ="";
    }
    $.ajax({
        type:"post",
        url:"/OutBoundManage/checkStatic",
        data:{checkhouse:checkhouse,ordernumber:ordernumber,state:state},
        dataType:"json",
        success:function(r){
            if (r.data != null){
                var t="";
                for(var i=0;i<r.data.length;i++){
                    t+="<tr outboundid='"+r.data[i].outboundid+"'>\n" +
                        "          <td>"+(i+1)+"</td>\n" +
                        "          <td class='orderid'>"+r.data[i].orderid+"</td>\n" +
                        "          <td>￥"+r.data[i].totalprice+"</td>\n" +
                        "          <td>"+r.data[i].storagename+"</td>\n" +
                        "          <td>"+r.data[i].outboundtime+"</td>\n" +
                        "          <td>"+r.data[i].outboundman+"</td>\n" +
                        "          <td stateid='"+r.data[i].stateid+"'>"+r.data[i].state+"</td>\n" +
                        "          <td>\n";
                    if(r.data[i].state==="未发货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定此订单发货吗？')\">发货</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已发货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen2(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单回款吗？')\">确认回款</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已回款"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定取消此订单吗？')\">取消订单</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="取消订单"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen1(this,'确定收到此订单的退货吗？')\">确认已退货</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="已退货"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick='toOrderDetailView(this)' class=\"tablelink\">查看详情</a>\n" +
                            "          </td>\n";
                    }
                    t+= "        </tr>\n";
                }
                $(".tablelist tbody").html(t);
                var pageNum = document.getElementById("pageNum");
                pageNum.innerHTML=r.pageNum
                var totalcomment = document.getElementById("totalcomment");
                totalcomment.innerHTML=r.totalRows
                totalPage=r.totalPage;
                getpagemodel(totalPage);
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}